package com.technoorbit.vbreakup;



import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
 
/**
 * This is the main Activity that displays the current chat session.
 */
public class BroadcastChat extends Activity {
    // Debugging
    private static final String TAG = "BcastChat";
    private static final boolean D = true;

    // Message types sent from the BluetoothChatService Handler
    public static final int MESSAGE_READ 	= 1;
    public static final int MESSAGE_WRITE 	= 2;
    public static final int MESSAGE_TOAST 	= 3;

    // Key names received from the BroadcastChatService Handler
    public static final String TOAST = "toast";

    // Intent request codes
    private static final int INTENT_GET_USERNAME = 1;
    
    // Layout Views
    private ListView 	mConversationView;
    private EditText 	mOutEditText;
    private Button 		mSendButton;

    // Array adapter for the conversation thread
    private ArrayAdapter<String> mConversationArrayAdapter;
    // String buffer for outgoing messages
    private StringBuffer mOutStringBuffer;
    // Member object for the chat services
    private BroadcastChatService mChatService = null;
    
    
    //user name
    private String mUserName = "Me";

    
    public void setUserName(String userName){
    	mUserName = userName;
    }
    
    
    // The Handler that gets information back from the BluetoothChatService
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
        	
        	if(D) Log.e(TAG, "[handleMessage !!!!!!!!!!!! ]");
        	
            switch (msg.what) {
            
	            case MESSAGE_WRITE:
	            	
	                byte[] writeBuf = (byte[]) msg.obj;
	                // construct a string from the buffer
	                String writeMessage = new String(writeBuf);
	                mConversationArrayAdapter.add(writeMessage);
	                break;
	            case MESSAGE_READ:
	                String readBuf = (String) msg.obj;
	                mConversationArrayAdapter.add(readBuf);
	                break;               
	            case MESSAGE_TOAST:
	                Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST),
	                               Toast.LENGTH_SHORT).show();
	                break;
            }
        }
    };    
    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(D) Log.e(TAG, "+++ ON CREATE +++");

        // Set up the window layout
        setContentView(R.layout.main);
    }

   
    public void onStart() {
        super.onStart();
        if(D) Log.e(TAG, "++ ON START ++");

        setupChat();
    }

    @Override
    public synchronized void onResume() {
        super.onResume();
        if(D) Log.e(TAG, "+ ON RESUME +");
        
        mChatService.start();
    }


    private void setupChat() {
        Log.d(TAG, "setupChat()");

        // Initialize the array adapter for the conversation thread
        mConversationArrayAdapter = new ArrayAdapter<String>(this, R.layout.message);
        mConversationView = (ListView) findViewById(R.id.in);
        mConversationView.setAdapter(mConversationArrayAdapter);

        // Initialize the compose field with a listener for the return key
        mOutEditText = (EditText) findViewById(R.id.edit_text_out);
        mOutEditText.setOnEditorActionListener(mWriteListener);

        // Initialize the send button with a listener that for click events
        mSendButton = (Button) findViewById(R.id.button_send);
        mSendButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	if(D) Log.e(TAG, "[sendButton clicked]");
                // Send a message using content of the edit text widget
                TextView view = (TextView) findViewById(R.id.edit_text_out);
                String message = view.getText().toString();
                sendMessage(message);                
            }
        });

        // Initialize the BluetoothChatService to perform bluetooth connections
        if(mChatService == null)
        	mChatService = new BroadcastChatService(this, mHandler);

        // Initialize the buffer for outgoing messages
        if(mOutStringBuffer == null)
        	mOutStringBuffer = new StringBuffer("");
    }
    
    
    public synchronized void onPause() {
        super.onPause();
        if(D) Log.e(TAG, "- ON PAUSE -");
        if (mChatService != null) mChatService.stop();
    }


    public void onStop() {
        super.onStop();
        if(D) Log.e(TAG, "-- ON STOP --");
        if (mChatService != null) mChatService.stop();
    }


    public void onDestroy() {
        super.onDestroy();
        if(D) Log.e(TAG, "--- ON DESTROY ---");
        // Stop the Broadcast chat services
        if (mChatService != null) mChatService.stop();
        
    }
    
    

    /**
     * Sends a message.
     * @param message  A string of text to send.
     */
    private void sendMessage(String message) {
    	
    	if(D) Log.e(TAG, "[sendMessage]");
    	
    		
        // Check that there's actually something to send
        if (message.length() > 0 ) {
        	message = mUserName +": "+ message ;    
            byte[] send = message.getBytes();
            mChatService.write(send);

            // Reset out string buffer to zero and clear the edit text field
            mOutStringBuffer.setLength(0);
            mOutEditText.setText(mOutStringBuffer);
        }
    }


    // The action listener for the EditText widget, to listen for the return key
    private TextView.OnEditorActionListener mWriteListener =
        new TextView.OnEditorActionListener() {
        public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
            // If the action is a key-up event on the return key, send the message
            if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_UP) {
                String message = view.getText().toString();
                sendMessage(message);
            }
            if(D) Log.i(TAG, "END onEditorAction");
            return true;
        }
    };
    
    
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    	
        if(D) Log.d(TAG, "onActivityResult " + resultCode);
        switch (requestCode) {
       
        case INTENT_GET_USERNAME:            
            // When user name dialog returns
            if (resultCode == Activity.RESULT_OK ) {
            	
            	mUserName = data.getExtras()
                .getString(UserNameActivity.EXTRA_USER_NAME);
            	
            	Toast.makeText(this, "User name changed to "+mUserName, Toast.LENGTH_SHORT).show();
            	
            } else {
                
                Toast.makeText(this, R.string.username_error, Toast.LENGTH_SHORT).show();
            }
        }
    }

    
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }


    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.username:
            // Launch the UserNameActivity to see devices and do scan
            Intent serverIntent = new Intent(this, UserNameActivity.class);
            startActivityForResult(serverIntent, INTENT_GET_USERNAME); 
            return true;
        }
        return false;
    }
}